package util;
import book.Book;
import com.bit.utils.FileUtils;
import constant.Constant;

import java.io.IOException;
import java.time.LocalDate;

public class AnalyzingBook {
    //storeObject:存储对象；
    //存储书籍到指定对象中；
    public void storeObject(Book[] books,String filename) {
        //先遍历books数组当中不为空的数据多少个?
        int bookUseLen=0;
        for(int i=0;i<books.length;i++){
            if(books[i]!=null){
                bookUseLen++;
            }
        }
        StringBuilder jsonArray=new StringBuilder();
        for(int i=0;i<bookUseLen;i++){
            if(books[i]!=null){
                jsonArray.append(books[i].toJSON());
                if(i!=bookUseLen-1){
                    jsonArray.append("\n");
                }
            }

        }
        //数据写⼊⽂件
        FileUtils.writeFile(jsonArray.toString(),filename);

    }
    //将字符串转换成书籍对象；
    private Book parseBookJson(String json){
        //1.判断该字符串是否为空；
        if(json.isEmpty()){
            return null;
        }
        //2.使用逗号分割出每个数据；
        String[] pairs=json.split(",");
        //3.每个属性进行转化赋值；
        int boolId=Integer.parseInt(pairs[0]);
        String title=pairs[1];
        String author=pairs[2];
        String category=pairs[3];
        int publishYear=Integer.parseInt(pairs[4]);
        boolean isBorrow=Boolean.parseBoolean(pairs[5]);
        int borrowCount = Integer.parseInt(pairs[6]);
        LocalDate shelfDate = LocalDate.parse(pairs[7]);
        //4.构造书籍对象；
        if(title!=null&&author!=null&&category!=null&& shelfDate!=null){
            Book book=new Book(title,author,category,publishYear,shelfDate);
            book.setBookId(boolId);
            book.setBorrowCount(borrowCount);
            book.setBorrowed(isBorrow);
        }
        return null;
    }

    //加载对象；
    public Book[] loadObject(String filename) {
        //1.从文件中读取数据；
        String content=FileUtils.readFile(filename);
        if(content==null||content.isEmpty()){
            System.out.println("File is null or does dont exit"+filename);
            return null;
        }
        //2.使用分隔符\n进行字符串分割；
        String[] bookJsonStrings=content.split("\n");
        //3.把对应的字符串组装成书籍对象；
        Book[] bookList=new Book[bookJsonStrings.length];
        for(int i=0;i< bookJsonStrings.length;i++){
            Book book=parseBookJson(bookJsonStrings[i]);
            bookList[i]=book;
        }
        return bookList;
    }
    public static void main(String[] args){
        AnalyzingBook myProperties=new AnalyzingBook();
        // 存储数据
        Book[] books = new Book[4];
        books[0] = new Book("java", "gaobo", "编程", 1994, LocalDate.of(2023, 9,
                24));
        books[1] = new Book("mysql", "lisi", "编程", 1999, LocalDate.of(2024, 2,
                10));
        books[2] = new Book("php", "gaobo", "编程", 2020, LocalDate.of(2023, 9,
                23));
        books[3] = new Book("西游记", "吴承恩", "⼩说", 2024, LocalDate.of(2023,
                9, 23));
        myProperties.storeObject(books, Constant.ALL_BOOK_FILE_NAME);
        Book[] loadedBooks= myProperties.loadObject(Constant.ALL_BOOK_FILE_NAME);
        if(loadedBooks!=null){
            System.out.println("Loade book");;
            for(Book book:loadedBooks){
                System.out.println(book);
            }
        }

    }
}
